﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FMS.Formulation.Licensing.Engine
{

    internal class WMIInfo
    {
        private WMIBiosInfo bios = new WMIBiosInfo();

        private WMIComputerSystemInfo pc = new WMIComputerSystemInfo();

        private WMILogicalDiskInfo logicalDisk = new WMILogicalDiskInfo();

        private WMINetworkAdapterInfo networkAdapter = new WMINetworkAdapterInfo();

        private WMIProcessorInfo processor = new WMIProcessorInfo();

        private string errorMessage = string.Empty;

        private StringDictionary _wmiDictionary = new StringDictionary();

        private string wmiInfo = string.Empty;

        public string ErrorMessage => errorMessage.Trim();

        public StringDictionary WMIDictionary => _wmiDictionary;

        public string WMIInformation => wmiInfo;

        private string LimitStringLength(string inString, int LimitLength)
        {
            string text = inString.Trim();
            if (text.Length > LimitLength)
            {
                return text.Remove(LimitLength);
            }
            return text;
        }

        public bool Read()
        {
            bool result = false;
            try
            {
                pc.Read();
                if (pc.MachineName.Trim().Length == 0)
                {
                    pc.MachineName = "[None]";
                }
                if (pc.DomainName.Trim().Length == 0)
                {
                    pc.DomainName = "[None]";
                }
                if (pc.UserName.Trim().Length == 0)
                {
                    pc.UserName = "[None]";
                }
                if (pc.WorkgroupName.Trim().Length == 0)
                {
                    pc.WorkgroupName = "[None]";
                }
                _wmiDictionary.Clear();
                _wmiDictionary.Add("MachineName", LimitStringLength(pc.MachineName, 255));
                _wmiDictionary.Add("DomainName", LimitStringLength(pc.DomainName, 255));
                _wmiDictionary.Add("UserName", LimitStringLength(pc.UserName, 255));
                _wmiDictionary.Add("Workgroup", LimitStringLength(pc.WorkgroupName, 255));
                bios.Read();
                if (bios.SerialNumber.Trim().Length == 0)
                {
                    bios.SerialNumber = "[None]";
                }
                _wmiDictionary.Add("BIOSSerialNumber", LimitStringLength(bios.SerialNumber, 255));
                processor.Read();
                if (processor.DeviceID.Trim().Length == 0)
                {
                    processor.DeviceID = "[None]";
                }
                if (processor.ProcessorName.Trim().Length == 0)
                {
                    processor.ProcessorName = "[None]";
                }
                _wmiDictionary.Add("ProcessorID", LimitStringLength(processor.DeviceID, 255));
                _wmiDictionary.Add("ProcessorName", LimitStringLength(processor.ProcessorName, 255));
                string text = networkAdapter.Read();
                if (text.Trim().Length == 0)
                {
                    text = "[None]";
                }
                _wmiDictionary.Add("NetworkAdapterMACAddress", LimitStringLength(text, 255));
                StringDictionary stringDictionary = new StringDictionary();
                StringBuilder stringBuilder = new StringBuilder();
                foreach (DictionaryEntry item in _wmiDictionary)
                {
                    string text2 = item.Key.ToString().Trim();
                    string text3 = item.Value.ToString();
                    if (text3 == string.Empty || text3 == null)
                    {
                        text3 = "[NOT DEFINED]";
                    }
                    stringDictionary.Add(text2, text3);
                    stringBuilder.Append(text2);
                    stringBuilder.Append("=");
                    stringBuilder.Append(LimitStringLength(text3, 255) + "\n");
                }
                _wmiDictionary = stringDictionary;
                wmiInfo = stringBuilder.ToString();
                result = true;
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            return result;
        }
    }

}
